﻿using System;
using Toolkit.Web.Infastructure;

namespace Toolkit.Web.ViewComponents.Notification
{
    public sealed class NotificationHtmlBuilder : ViewComponentHtmlBuilderBase
    {
        public NotificationHtmlBuilder(Notification notification)
            : base(notification)
        {
            if (notification == null)
            {
                throw new ArgumentNullException("notification");
            }

            _notification = notification;
            CssClassNames = new ComponentCssClassNames();
        }

        private Notification _notification
        {
            get;
            set;
        }

        public ComponentCssClassNames CssClassNames
        {
            get;
            private set;
        }

        public override IHtmlNode Html()
        {
            if (_notification.MessageList.Count == 0 && _notification.HideNotificationIfThereAreNoMessages)
            {
                return new TextNode(string.Empty);
            }
            return NotificationTag();
        }

        public IHtmlNode NotificationTag()
        {
            IHtmlNode htmlTag = new HtmlNode("div");
            htmlTag.MergeAttributes(_notification.HtmlAttributes, true);

            // Add default class names.
            htmlTag.AddClass(CssClassNames.ComponentWrapper);

            // Add notification type class name.
            if (_notification.Type == NotificationType.Informative)
            {
                htmlTag.AddClass(CssClassNames.Informative);
            }
            else if (_notification.Type == NotificationType.Success)
            {
                htmlTag.AddClass(CssClassNames.Success);
            }
            else if (_notification.Type == NotificationType.Error)
            {
                htmlTag.AddClass(CssClassNames.Error);
            }
            else if (_notification.Type == NotificationType.Warning)
            {
                htmlTag.AddClass(CssClassNames.Warning);
            }

            if (_notification.Title != null && _notification.Title.HasValue())
            {
                htmlTag.Append(TitleTag());
            }

            if (_notification.MessageList != null && _notification.MessageList.Count > 0)
            {
                htmlTag.Append(MessageTag());
            }

            return htmlTag;
        }

        public IHtmlNode TitleTag()
        {
            IHtmlNode htmlTag = new HtmlNode("div");
            htmlTag.AddClass(CssClassNames.Title);

            IHtmlNode iconTag = new HtmlNode("span");
            iconTag.AddClass("vc-icon");
            htmlTag.Append(iconTag);

            IHtmlNode textTag = new HtmlNode("span");
            textTag.AddClass("vc-text");
            textTag.ApplyTemplate(_notification.Title);
            htmlTag.Append(textTag);

            return htmlTag;
        }

        public IHtmlNode MessageTag()
        {
            IHtmlNode htmlTag = new HtmlNode("div");
            htmlTag.AddClass(CssClassNames.Content);
            foreach (HtmlTemplate template in _notification.MessageList)
            {
                IHtmlNode messageNode = new HtmlNode("div");
                messageNode.AddClass(CssClassNames.Message);
                messageNode.ApplyTemplate(template);
                messageNode.AppendTo(htmlTag);
            }
            return htmlTag;
        }

        public class ComponentCssClassNames
        {
            public readonly string ComponentWrapper = "vc-notification";
            public readonly string Informative = "vc-notification-informative";
            public readonly string Success = "vc-notification-success";
            public readonly string Error = "vc-notification-error";
            public readonly string Warning = "vc-notification-warning";
            public readonly string Title = "vc-notification-title";
            public readonly string Content = "vc-notification-content";
            public readonly string Message = "vc-notification-message";
        }
    }
}
